package com.foocus.foocuscloud.provider.user.controller;


import com.foocus.foocuscloud.common.aop.log.LogWrite;
import com.foocus.foocuscloud.common.domain.ResponseWrapper;
import com.foocus.foocuscloud.provider.user.domain.LoginUser;
import com.foocus.foocuscloud.provider.user.service.LoginUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/login")
public class LoginUserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LoginUserController.class);

    @Resource
    private LoginUserService userService;

    @LogWrite(value = "根据userId查询用户")
    @GetMapping("/get/{userId}")
    @ResponseBody
    public ResponseWrapper getLoginUser(@PathVariable("userId") String userId) {

        LoginUser loginUser = userService.getLoginUser(userId);
        return ResponseWrapper.Success(loginUser);
    }

    @LogWrite(value = "新用户注册")
    @PostMapping("/register/user")
    @ResponseBody
    public ResponseWrapper registerLoginUser(@RequestBody LoginUser user) {

        userService.registerLoginUser(user);
        return ResponseWrapper.Success();
    }

    @LogWrite(value = "用户登录")
    @PostMapping("/userLogin")
    @ResponseBody
    public ResponseWrapper loginUser(@RequestBody LoginUser user) {

        LoginUser loginUser = userService.loginUser(user);
        return ResponseWrapper.Success(loginUser);
    }

    @LogWrite(value = "用户注销")
    @PostMapping("/userLogout")
    @ResponseBody
    public ResponseWrapper logoutUser(@RequestParam("userId") String userId) {

        Boolean boo = userService.logoutUser(userId);
        return boo ? ResponseWrapper.Success() : ResponseWrapper.Fail();
    }
}
